//app.js
var domain = "https://mall.lxd.org.cn/api"; //接口地址 仅用于测试
// var domain = 'http://wn.t.acrabsoft.com'
var errimg = "/images/error.jpg"; //错误图
App({
	onLaunch: function() {;
		var that = this;
		that.login();
	},
	login: function() {
		var that = this;
		//用户授权信
		wx.login({
			success: function(res2) {
				var code = res2.code;
				wx.request({
					url: domain + '/api/wechat/login',
					data: {
						code: code
					}, // 设置请求的 参数
					success: function(e) {
						if (e.data.code == 1) {
							wx.setStorage({
								key: "sessionKey",
								data: e.data.sessionKey //需要存储的数据
							})
						}
					},
					fail: function(e) {
						console.log('app.js line103', res)
					}
				})
			},
			fail: function(res) {
				console.log('app.js line108', res)
			}
		})
	},
	onLoad: function() {

	},
	globalData: {
		userInfo: null,
		domain: domain,
		errimg: errimg
	},
	globalAjax: function(url, method, data, call, head) { //全局接口方法 1url地址，2接口类型，3data参数，4回调，5header
		var data = data || {};
		var method = method || "get";
		var head1 = {
			token: wx.getStorageSync('token')
		};
		wx.showLoading({
		    title: '加载中' // 数据请求前loading
		});
		wx.request({
			url: domain + url,
			header: head1,
			data: data,
			method: method,
			success: function(res) {
				if(res.data.code==-1){
					wx.showModal({
						title:'温馨提示',
						content:'您还没有登录,请先登录',
						success:function(r){
							if(r.confirm){
								wx.reLaunch({
									url: '/pages/home/login/login'
								})
							}
							else{
								wx.navigateBack()
							}
						}
					})
					return
				}
				if (typeof res.data === "object") {
					call(res.data)
				}
				wx.hideLoading()
			},
			fail: function(res) {
				wx.showToast({
					title: '接口加载错误',
					icon: "none"
				});
				wx.hideLoading()
				// fail调用接口失败
			},
			complete: function() {
				wx.hideLoading()
			}
		});
	},
	pays: function(data, call, call2) { //同意支付调用
		wx.requestPayment({
			timeStamp: data.timeStamp,
			nonceStr: data.nonceStr,
			package: data.package,
			signType: 'MD5',
			paySign: data.paySign,
			fail: function(aaa) {
				wx.showToast({
					title: '支付失败',
					icon: "none"
				})
				call2(aaa);
			},
			success: function(e) {
				wx.showToast({
					title: '支付成功'
				})
				call(e);
			}
		})
	}
})
